/*
 * @lc app=leetcode.cn id=350 lang=javascript
 *
 * [350] 两个数组的交集 II
 */

// @lc code=start
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersect = function(nums1, nums2) {
    nums1.sort((a,b) => a - b);
    nums2.sort((a,b) => a - b);

    let len1 = nums1.length;
    let len2 = nums2.length;
    let p1 = 0;
    let p2 = 0;
    const res = [];
    while(p1 < len1 && p2 < len2) {
        const num1 = nums1[p1];
        const num2 = nums2[p2];
        if (num1 === num2) {
            res.push(num1);
            p1++;
            p2++;
        } else if (num1 < num2) {
            p1++;
        } else {
            p2++;
        }
    }
    return res;
};
// @lc code=end

